package com.zlzlib.libui.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.zlzlib.libui.util.AppSystemUtil;


/**
 * @Desc: 前台服务功能
 * @Copyright:
 * @DateTime: 2021/11/16 10:50
 * @Author zlz
 * @Version 1.0
 */
public abstract class BaseAliveService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //保活
        keepAlive();
        return super.onStartCommand(intent, flags, startId);
    }

    protected int getForegroundId() {
        //如果设置为0 则不显示到通知栏
        return 1;
    }

    protected abstract int getIcon();

    protected abstract Class<?> getOpenClass();

    private void keepAlive() {
        try {
            // 设置为前台服务避免kill
            startForeground(getForegroundId(), AppSystemUtil.getNotification(getBaseContext(), "BaseAliveService",
                    "BaseAliveService", "BaseAliveService", getOpenClass(), getIcon(), null));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}